<template>
  <div class="app-container"
       v-loading.fullscreen.lock="allLoading"
       element-loading-text="拼命处理中,请耐心等待"
       element-loading-spinner="el-icon-loading"
       element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-dialog :close-on-click-modal="changeBitFlagIsNone" :title="mainDialogInfo.title" :visible.sync="showDialog"
               width="700px" append-to-body @close="closeDialog">
      <el-form ref="form" :disabled="changeBitFlagIsNone" :model="form" :rules="rules" label-width="120px">
      </el-form>
      <el-row :gutter="10" class="mb8">
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="el-icon-delete"-->
<!--            size="mini"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--          >删除</el-button>-->
<!--        </el-col>-->
        <right-toolbar :search="false" @queryTable="getMainList"></right-toolbar>
      </el-row>
      <el-table v-loading="lookupTable.loading" :data="lookupTable.list" :show-header="false"  highlight-current-row @row-dblclick="handleSelect">
        <el-table-column label="标题" align="left" prop="title" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="center" prop="created_at" width="180" :formatter="formatterTimeYmdHm"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="50">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelMainDialog">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { sksDialogFormMixin } from 'sks-plugin-el-erp/lib/dialog-form'
import { mergeFromObject } from 'sks-plugin-el-erp/lib/core'
import { newSksTable } from 'sks-plugin-el-erp/lib/sks-model-utils'
import { deleteChat, getChatHistoryList } from '@/api/chat'
import { delKnowledge } from '@/api/knowledge'

//用于选择模型
export default {
  name: "ShowChatHistoryDialog",
  components: {
  },
  mixins: [
    sksDialogFormMixin,
  ],
  data() {
    return {
      defaultForm: {
        id: undefined,
        ai_model_id: undefined,
        name: undefined,
        description: undefined,
        emoji: undefined,
        tools: undefined,
        group: undefined,
        agent_ids: undefined,
        system_prompt: undefined,
        summarize_prompt: undefined,
        created_at: undefined,
        updated_at: undefined,
      },
      defaultFormNotNull: {
        virtualDocumentList: [],
      },

      rules: {
      },
      lookupTable: mergeFromObject(newSksTable(),{
        queryParams:{
          pageNum: 1,
          pageSize: 9999,
          ai_api_id: undefined,
        },
      })
    };
  },
  methods:{
    showToSelect({extra,dialogInfo,virtualChangeBitFlag=this.sksUtils.CHANGE_BIT_FLAG.none}){
      let data={}
      this.getMainList()
      this.show({
        extra: extra,
        form: this.sksUtils.mergeFromObject({},data,{
          virtualChangeBitFlag: virtualChangeBitFlag,
        }),
        dialogInfo: dialogInfo
      })

    },
    getMainList(){
      let queryParams = this.lookupTable.queryParams;
      this.sksUtils.resetSksTableBefore(this.lookupTable)
      getChatHistoryList(queryParams).then(res=>{
        this.sksUtils.setSksTableDataByPage(this.lookupTable,res)
      })

    },
    handleDelete(row){
      // const ids = row.id?[row.id]:this.ids;
      const ids = [row.id];
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return deleteChat(ids);
      }).then(() => {
        this.getMainList()
        if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.deleteSuccessCallback)) {
          this.mainDialogInfo.deleteSuccessCallback({chatIds: ids})
          return
        }
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleSelect(row){
      let data = this.sksUtils.mergeFromObject({}, row);
      if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.selectCallback)) {
        this.mainDialogInfo.selectCallback({selectRow: data, ref: this})
        return
      }
      this.closeDialog()
    },
  }
}
</script>

<style scoped>
</style>
